﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Bamboo.Utils
{
    public static class Encrypt
    {
        /// <summary>
        /// 密文的前缀
        /// </summary>
        public const string DEFAULT_KEY = "huyd!@#78.";
        public const string ENCRYPTED_PREFIX = "ENCRYPT:";
        /// <summary>
        /// 3DES解密
        /// </summary>
        /// <param name="input">解密字符串</param>
        /// <param name="key">解密key，为空则使用默认密钥</param>
        /// <returns></returns>
        public static string DES3Decrypt(string input, string key = "")
        {
            if (input == null)
                return null;
            if (!input.StartsWith(ENCRYPTED_PREFIX))
                return input;
            if (string.IsNullOrWhiteSpace(key))
            {
                key = DEFAULT_KEY;
            }
            string text = input.Substring(ENCRYPTED_PREFIX.Length);

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            DES.Key = hashMD5.ComputeHash(Encoding.Default.GetBytes(key));
            DES.Mode = CipherMode.ECB;
            ICryptoTransform DESDecrypt = DES.CreateDecryptor();
            string result = "";
            try
            {
                byte[] Buffer = Convert.FromBase64String(text);
                result = Encoding.Default.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }
            catch (System.Exception e)
            {
                throw new Exception("解密失败：" + input, e);
            }
            return result;
        }

        public static bool IsEncrypted(string text)
        {
            if (text == null)
                return false;
            return text.StartsWith(ENCRYPTED_PREFIX);
        }

        /// <summary>
        /// 3DES加密
        /// </summary>
        /// <param name="input">需要加密的字符串</param>
        /// <param name="key">加密key，为空则使用默认密钥</param>
        /// <returns></returns>
        public static string DES3Encrypt(string input, string key = "")
        {
            if (input == null)
                return null;
            if (input.StartsWith(ENCRYPTED_PREFIX))
                return input;
            if (string.IsNullOrWhiteSpace(key))
            {
                key = DEFAULT_KEY;
            }
            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
            DES.Key = hashMD5.ComputeHash(Encoding.Default.GetBytes(key));
            DES.Mode = CipherMode.ECB;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = Encoding.Default.GetBytes(input);
            return ENCRYPTED_PREFIX + Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
        }


        public static string MakeMD5(string input)
        {
            var md5 = new MD5CryptoServiceProvider();
            var v1 = md5.ComputeHash(Encoding.Default.GetBytes(input));
            var v2 = new StringBuilder(128);
            foreach (var item in v1)
            {
                v2.Append(item.ToString("x2"));
            }
            return v2.ToString();
        }
    }
}
